package com.example.config.sitemesh;

import org.sitemesh.DecoratorSelector;
import org.sitemesh.config.PathBasedDecoratorSelector;
import org.sitemesh.content.Content;
import org.sitemesh.webapp.WebAppContext;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 基于request参数decorator值进行动态定位装饰器的选择器
 * 如果decorator参数有值，则返回"/WEB-INF/views/layouts/" + decorator + ".jsp"作为目标装饰模板页面
 */
public class ParamDecoratorSelector extends PathBasedDecoratorSelector<WebAppContext> {

    private DecoratorSelector<WebAppContext> defaultDecoratorSelector;

    public ParamDecoratorSelector(DecoratorSelector<WebAppContext> defaultDecoratorSelector) {
        this.defaultDecoratorSelector = defaultDecoratorSelector;
    }

    @Override
    public String[] selectDecoratorPaths(Content content, WebAppContext context) throws IOException {
        // build decorator based on the request
        HttpServletRequest request = context.getRequest();
        String decorator = request.getParameter("decorator");
        if ("decorator_blank".equals(decorator)) {
            //按照参数值返回对应路径下面的ftl装饰模板页码
            return new String[] { "/decorators/decorator-no-header-footer.ftl" };
        }

        // Otherwise, fallback to the standard configuration
        String[] strings = defaultDecoratorSelector.selectDecoratorPaths(content, context);
        return strings;
    }
}